package com.hslxy.niukewang.controller.interceptor;

import com.hslxy.niukewang.entity.LoginTicket;
import com.hslxy.niukewang.entity.User;
import com.hslxy.niukewang.service.DataService;
import com.hslxy.niukewang.util.CookieUtil;
import com.hslxy.niukewang.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

@Component
public class DataInterceptor implements HandlerInterceptor {
    @Autowired
    private DataService dataService;

    @Autowired
    private HostHolder hostHolder;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        dataService.recordUV(request.getRemoteHost());
        if(hostHolder.getUsers() != null){
            dataService.recordDAV(hostHolder.getUsers().getId());
        }
        return true;
    }
}
